'use strict'

import {app, BrowserWindow, ipcMain, globalShortcut } from 'electron'
import './ipcMain'
import './tray'
import {setTray} from './tray'
import DownLoad from "./downLoad";
const path = require('path')
const fs = require('fs')
const isDev = (process.env.NODE_ENV === 'development')
const isMac = (process.platform === 'darwin')
/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (!isDev) {
  global.__static = path.join(__dirname, '/static').replace(/\\/g, '\\\\')
}

const staticPath = path.join(__static, '')

let mainWindow
const winURL = isDev ? `http://127.0.0.1:9088` : `file://${__dirname}/index.html`

function createWindow () {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    height: 600,
    useContentSize: true,
    width: 900,
    transparent: true,
    frame: false,
    kiosk: false,
    resizable: true,
    shadow: false,
    // skipTaskbar: true,
    // alwaysOnTop: false,
    backgroundColor: '#00000000',
    show: false,
    webPreferences: {
      webSecurity: false,
      nodeIntegration: true,
      contextIsolation: false,
      enableRemoteModule: true,
      preload: path.join(__static, '/js/preload.js' || '.')
    }

  })
  mainWindow.once('ready-to-show', () => {
    mainWindow.show()
  })
  // mainWindow.setIgnoreMouseEvents(true, { forward: true })
  // mainWindow.setMovable(false)
  // mainWindow.setResizable(false)
  if(!isDev){
    // mainWindow.hide()
    // mainWindow.setAlwaysOnTop(true)
    // mainWindow.setSkipTaskbar(true)
    if(isMac) app.dock.hide();
    globalShortcut.register('ctrl+f12', function () { mainWindow.webContents.openDevTools()})
  }else{
    mainWindow.webContents.openDevTools();
  }
  // mainWindow.webContents.openDevTools();
  //mainWindow.setIgnoreMouseEvents(true)
  mainWindow.loadURL(winURL)

  mainWindow.on('closed', () => {
    mainWindow = null
  })
  setTray(mainWindow)

  // autoUpdateInit(mainWindow);

  let userData = app.getPath('userData')

  //创建tags目录
  let tagDir = userData + '\\tags'
  if(!fs.existsSync(tagDir)){
    console.log('目录不存在， 创建：' + tagDir)
    fs.mkdir(tagDir, null, function (error) {
      if(error){
        console.log('创建失败：' + error)
      }
    });
    console.log()
  }

  DownLoad.init(mainWindow);
  // mainWindow.webContents.on("dom-ready",()=>{
  //   mainWindow.webContents.send('init')
  // })
  //
  // if(isDev)
  //
}

app.on('ready', function () {
  createWindow()
  // if (!isDev){
  // }
  // getSettings()


})

// app.on('window-all-closed', () => {
//   if (process.platform !== 'darwin') {
//     app.quit()
//   }
// })

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
